import { defineStore } from "pinia";
import { getRouters } from "@/api/menu";
import Layout from "@/layout/index.vue";
import auth from "@/plugins/auth";
import router, { constantRoutes } from "@/router";

export const usePermissionStore = defineStore({
  id: "permission",
  state: () => ({
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
  }),
  getters: {},
  actions: {
    GenerateRoutes() {
      return new Promise((resolve) => {
        getRouters().then((res) => {
          const sdata = JSON.parse(JSON.stringify(res.data));
          const rdata = JSON.parse(JSON.stringify(res.data));
          const sidebarRoutes = filterAsyncRouter(sdata);
          const rewriteRoutes = filterAsyncRouter(rdata, false, true);
          //const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
          rewriteRoutes.push({ path: "*", redirect: "/404", hidden: true });
          // router.addRoutes(asyncRoutes);
          this.addRoutes = rewriteRoutes;
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          this.routes = constantRoutes.concat(rewriteRoutes);
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          this.defaultRoutes = constantRoutes.concat(sidebarRoutes);
          this.topbarRouters = sidebarRoutes;
          this.sidebarRouters = sidebarRoutes;
          resolve(rewriteRoutes);
        });
      });
    },
  },
});
function filterAsyncRouter(
  asyncRouterMap: any,
  lastRouter = false,
  type = false
) {
  return asyncRouterMap.filter((route: any) => {
    if (type && route.children) {
      route.children = filterChildren(route.children);
    }
    if (route.component) {
      // Layout ParentView 组件特殊处理
      if (route.component === "Layout") {
        route.component = Layout;
      }
      // else if (route.component === 'ParentView') {
      //   route.component = ParentView
      // } else if (route.component === 'InnerLink') {
      //   route.component = InnerLink
      // }
      else {
        route.component = loadView(route.component);
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children, route, type);
    } else {
      delete route["children"];
      delete route["redirect"];
    }
    return true;
  });
}
function filterChildren(childrenMap: any, lastRouter = false) {
  let children: any = [];
  childrenMap.forEach((el: any, index: any) => {
    if (el.children && el.children.length) {
      if (el.component === "ParentView" && !lastRouter) {
        el.children.forEach((c: any) => {
          c.path = el.path + "/" + c.path;
          if (c.children && c.children.length) {
            children = children.concat(filterChildren(c.children, c));
            return;
          }
          children.push(c);
        });
        return;
      }
    }
    if (lastRouter) {
      el.path = (lastRouter as any).path + "/" + el.path;
    }
    children = children.concat(el);
  });
  return children;
}
export function filterDynamicRoutes(routes: any) {
  const res: any = [];
  routes.forEach((route: any) => {
    if (route.permissions) {
      if (auth.hasPermiOr(route.permissions)) {
        res.push(route);
      }
    } else if (route.roles) {
      if (auth.hasRoleOr(route.roles)) {
        res.push(route);
      }
    }
  });
  return res;
}
export const loadView = (view: any) => {
  // 使用 import 实现生产环境的路由懒加载
  return () => import(`@/views/${view}.vue`);
};
